﻿using System;
using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsGiggle.Core.Tfs.Implementation
{
    class ChangesetImpl : IChangeset
    {
        private Changeset _changeset;
        private Lazy<IChange[]> _changes;
        private Lazy<IWorkitem[]> _workItems;

        public ChangesetImpl(IVersionControlServer versionControlServer, Changeset changeset)
        {
            _changeset = changeset;

            _changes = new Lazy<IChange[]>(() =>
                {
                    return (from p in changeset.Changes select new ChangeImpl(versionControlServer, p)).ToArray();
                });

            _workItems = new Lazy<IWorkitem[]>(() =>
                {
                    return (from p in changeset.WorkItems select new WorkitemImpl(p.Id, p.Title)).ToArray();
                });
        }

        public IChange[] Changes
        {
            get { return _changes.Value; }
        }

        public int ChangesetId
        {
            get { return _changeset.ChangesetId; }
        }

        public string Committer
        {
            get { return _changeset.Committer; }
        }

        public string Comment
        {
            get { return _changeset.Comment; }
        }

        public IWorkitem[] WorkItems
        {
            get { return _workItems.Value; }
        }
    }
}